/*************************************************************************
	> File Name: Observer.h
	> Author: Joker
	> Mail: mahongtao@foxmail.com 
	> Created Time: 2020-09-08 09:11:49
 ************************************************************************/

#ifndef __OBSERVER_H__
#define __OBSERVER_H__

#include <list>
using std::list;

typedef int STATE;

class Observer;

//Subject抽象基类,只需要知道Observer基类的声明
class Subject{
public:
    Subject()
        :_SubjectState(-1){}
    virtual ~Subject();

    void Notify();                      //通知对象改编状态
    void Attach(Observer *pObserver);   //新增对象
    void Detach(Observer *pObserver);   //删除对象

    virtual void SetState(STATE nState);    //设置状态
    virtual STATE GetState();               //得到状态
protected:
    STATE _SubjectState;
    list<Observer *> _pListObserver;
};

class Observer{
public:
    Observer()
        :_ObserverState(-1){}
    virtual ~Observer(){};

    virtual void Update(Subject *pSubject) = 0;
protected:
    STATE _ObserverState;
};

class ConcreateSubject
:public Subject{
public:
    ConcreateSubject()
        :Subject(){}
    virtual ~ConcreateSubject(){}

    virtual void SetState(STATE nState);
    virtual STATE GetState();
};

class ConcreateObserver
:public Observer{
public:
    ConcreateObserver()
        :Observer(){}
    virtual ~ConcreateObserver(){}

    virtual void Update(Subject *pSubject);
};

#endif
